package com.yxp.offer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : yang.xp
 * @date : 2021/9/6 9:31
 * @description : 剑指 Offer 57 - II. 和为s的连续正数序列
 * <p>
 * 输入一个正整数 target ，输出所有和为 target 的连续正整数序列（至少含有两个数）。
 * 序列内的数字由小到大排列，不同序列按照首个数字从小到大排列。
 */
public class Problem57_2 {

    public static void main(String[] args) {
        Problem57_2 problem57_2 = new Problem57_2();
        System.out.println(problem57_2.findContinuousSequence(10));
    }


    public int[][] findContinuousSequence(int target) {
        List<int[]> result = new ArrayList<>();
        for (int i = 1, h = 0; i < target; i++, h++) {
            List<Integer> list = new ArrayList<>();
            int sum = 0;
            for (int j = i, m = 0; sum < target; j++, m++) {
                sum += j;
                list.add(j);
            }
            if (sum == target && list.size() > 1) {
                int[] res = new int[list.size()];
                for (int n = 0; n < list.size(); n++) {
                    res[n] = list.get(n);
                }
                result.add(res);
            }
        }
        int[][] resultArray = new int[result.size()][];
        for (int n = 0; n < result.size(); n++) {
            resultArray[n] = result.get(n);
        }
        return resultArray;
    }
}
